#!/usr/bin/env python

import time

import web

from redarrow.dispatcher import Config, run_command
from redarrow.command import prepare_option_parser, jsonize_result

urls = (
    '/command/(.*)', 'command',
)

class command:
    def GET(self, name):
        result = run_command(config, name)
        json_result = jsonize_result(result, pretty=options.debug)
        if not options.debug:
            web.header('Content-Type', 'application/json')
        web.output(json_result)
        
def main():
    parser = prepare_option_parser(usage="%prog [options] [port]")
    parser.add_option('-d', '--debug', action='store_true',
                      help="Return text/html instead of application/json")
    global options
    options, args = parser.parse_args()

    # re-construct sys.argv for web.py use
    import sys
    sys.argv = [sys.argv[0]] + args

    global config
    config = Config(options.config_file)

    web.run(urls, globals())
